package com.example.combwx.mvp.presenter;

import android.util.Log;

import com.example.combwx.db.UserUtility;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.presenter.BasePresenter;
import com.example.combwx.mvp.contract.RegisterContract;
import com.example.combwx.mvp.model.entity.ResponseUserEntity;
import com.example.combwx.mvp.model.entity.UserEntity;
import com.example.combwx.utils.LogUtils;
import com.example.combwx.utils.ToasetUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterPresenter extends BasePresenter<RegisterContract.IRegisterModel, RegisterContract.IRegisterView> {

    private static final String TAG = "RegisterPresenter";

    @Inject
    public RegisterPresenter(RegisterContract.IRegisterModel model, RegisterContract.IRegisterView view) {
        super(model, view);
    }


    public void getLogin(String phone, String pwd, int code) {
//        if (pwd.isEmpty()) {
//            ToasetUtils.showMsg("密码不为空");
//            return;
//        }
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("pwd", pwd);
        map.put("code", code);
        m.register(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void getRegis(String name, String path, int sex, int code) {
        List<UserEntity> userEntities = null;
        try {
            userEntities = UserUtility.getInstance().getDaoSession().loadAll(UserEntity.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        com.blankj.utilcode.util.LogUtils.json(userEntities);
        Map<String, Object> map = new HashMap<>();
        if (userEntities.get(0).user_name.isEmpty())
            return;
        map.put("user_id", userEntities.get(0).getUser_id());
        map.put("user_phone", userEntities.get(0).getUser_phone());
        map.put("user_pwd", userEntities.get(0).getUser_pwd());
        map.put("user_name", name);
        map.put("user_sex", sex);
        map.put("user_img", path);
        map.put("user_integral", 1);
        map.put("code", code);

        m.register(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseEntity entity) {
                        LogUtils.show("onNext" + entity.toString());
                        v.regirefres(entity);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @Override
    public void onNext(BaseEntity entity) {

        if (entity instanceof ResponseUserEntity) {
            ResponseUserEntity responseUserEntity = (ResponseUserEntity) entity;
            Log.i(TAG, "onNext:1111 " + responseUserEntity.getValues());
            try {
                UserUtility.getInstance().getDaoSession().insert(responseUserEntity.getValues());
                Log.i(TAG, "onNext: " + responseUserEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        v.regirefres(entity);
    }
}
